﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetBay.Core.BusinessEntities.Barcodes;
using Rio.Doc.Business.Repository.Translator;
using Rio.Doc.DataAccess;

namespace Rio.Doc.Business.Repository
{
    public class CABRepository
    {
        #region Interface Setting

        #region Add

        /// <summary>
        /// Adds the barcode.
        /// </summary>
        /// <param name="barcode">The barcode.</param>
        /// <returns></returns>
        public static long AddBarcode(Barcode barcode)
        {
            return DA_CAB.AddBarcode(barcode);
        }

        /// <summary>
        /// Adds the barcode separator.
        /// </summary>
        /// <param name="barcodeSeparator">The barcode separator.</param>
        /// <returns></returns>
        public static long AddBarcodeDocumentSeparator(BarcodeDocumentSeparator barcodeSeparator)
        {
            return DA_CAB.AddBarcodeDocumentSeparator(barcodeSeparator);
        }

        /// <summary>
        /// Adds the barcode folder separator.
        /// </summary>
        /// <param name="barcodeSeparator">The barcode separator.</param>
        /// <returns></returns>
        public static long AddBarcodeFolderSeparator(BarcodeFolderSeparator barcodeSeparator)
        {
            return DA_CAB.AddBarcodeFolderSeparator(barcodeSeparator);
        }

        #endregion Add

        #region Delete

        /// <summary>
        /// Deletes the barcode.
        /// </summary>
        /// <param name="barcodeId">The barcode id.</param>
        /// <returns></returns>
        public static bool DeleteBarcode(long barcodeId)
        {
            return DA_CAB.DeleteBarcode(barcodeId);
        }

        /// <summary>
        /// Deletes the barcode List.
        /// </summary>
        /// <param name="barcodeId">The barcode id List.</param>
        /// <returns></returns>
        public static bool DeleteBarcodeList(string p_sBarcodeIdList)
        {
            return DA_CAB.DeleteBarcodeList(p_sBarcodeIdList);
        }

        /// <summary>
        /// Deletes the document barcode separator.
        /// </summary>
        /// <param name="Identifier">The identifier.</param>
        /// <returns></returns>
        public static bool DeleteDocumentBarcodeSeparator(long Identifier)
        {
            return DA_CAB.DeleteDocumentBarcodeSeparator(Identifier);
        }

        /// <summary>
        /// Deletes the folder barcode separator.
        /// </summary>
        /// <param name="Identifier">The identifier.</param>
        /// <returns></returns>
        public static bool DeleteFolderBarcodeSeparator(long Identifier)
        {
            return DA_CAB.DeleteFolderBarcodeSeparator(Identifier);
        }

        #endregion Delete

        #region Get

        /// <summary>
        /// Gets the barcodes.
        /// </summary>
        /// <returns></returns>
        public static List<Barcode> GetBarcodes(bool withEnabled = false)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABRepository.GetBarcodes (withEnabled <" + withEnabled.ToString() + ">)...");

            //Variable initialization.
            var barcodes = CABTranslator.TranslateBarcodes(DA_CAB.GetBarcodes(withEnabled));

            try
            {
                foreach (var item in barcodes)
                {
                    item.BarCodeSeparatorList.AddRange(GetBarcodeFolderSepartorsById(item.Identifier));
                    item.BarCodeSeparatorList.AddRange(GetBarcodeDocumentSepartorsById(item.Identifier));
                }

                foreach (var i in barcodes)
                {
                    foreach (var j in i.BarCodeSeparatorList)
                    {
                        j.ForeignKey = i.Key;
                    }
                }
            }

            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABRepository.GetBarcodes Exception [" + ex.Message + "]");
                barcodes.Add(new Barcode("Exception in GetBarcodes : " + ex.Message));              
            }
            
            return barcodes;
        }

        /// <summary>
        /// Gets the folder separator barcodes.
        /// </summary>
        /// <returns></returns>
        public static List<Barcode> GetFolderSeparatorBarcodes()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABRepository.GetFolderSeparatorBarcodes()...");

            //Variable initialization.
            var barcodes = CABTranslator.TranslateBarcodes(DA_CAB.GetBarcodes(true));

            try
            {
                barcodes = barcodes.Where(p => p.Function == BarcodeSeparatorType.FolderSeparator).ToList();
                foreach (var item in barcodes)
                {
                    item.BarCodeSeparatorList.AddRange(GetBarcodeFolderSepartorsById(item.Identifier));
                }

                foreach (var i in barcodes)
                {
                    foreach (var j in i.BarCodeSeparatorList)
                    {
                        j.ForeignKey = i.Key;
                    }
                }
            }

            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABRepository.GetBarcodes Exception [" + ex.Message + "]");
                barcodes.Add(new Barcode("Exception in GetBarcodes : " + ex.Message));
            }

            return barcodes;
        }

        /// <summary>
        /// Gets the barcodes.
        /// </summary>
        /// <returns></returns>
        public static List<Barcode> GetAllTheBarCodes()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABRepository.GetAllTheBarCodes (No parameters)...");

            //Variable initialization.
            var barcodes = CABTranslator.TranslateBarcodes(DA_CAB.GetAllTheBarCodes());

            try
            {
                foreach (var item in barcodes)
                {
                    item.BarCodeSeparatorList.AddRange(GetBarcodeFolderSepartorsById(item.Identifier));
                    item.BarCodeSeparatorList.AddRange(GetBarcodeDocumentSepartorsById(item.Identifier));
                }

                foreach (var i in barcodes)
                {
                    foreach (var j in i.BarCodeSeparatorList)
                    {
                        j.ForeignKey = i.Key;
                    }
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABRepository.GetAllTheBarCodes Exception [" + ex.Message + "]");
                barcodes.Add(new Barcode("Exception in GetAllTheBarCodes : " + ex.Message));     
            }

            return barcodes;
        }

        /// <summary>
        /// Gets the barcode by id.
        /// </summary>
        /// <param name="barcodeId">The barcode id.</param>
        /// <returns></returns>
        public static List<Barcode> GetBarcodeById(long barcodeId)
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("CABRepository.GetBarcodeById (barcodeId <" + barcodeId.ToString() + ">)...");

            //variable initialization.
            var barcodes = CABTranslator.TranslateBarcodes(DA_CAB.GetBarcodeById(barcodeId));

            try
            {
                foreach (var item in barcodes)
                {
                    item.BarCodeSeparatorList.AddRange(GetBarcodeFolderSepartorsById(item.Identifier));
                    item.BarCodeSeparatorList.AddRange(GetBarcodeDocumentSepartorsById(item.Identifier));
                }
            }
            catch (Exception ex)
            {
                //Error.
                NetBay.Core.Log.LogManager.Instance().LogError("CABRepository.GetBarcodeById Exception [" + ex.Message + "]");
                barcodes.Add(new Barcode("Exception in GetBarcodeById : " + ex.Message));  
            }
            
            return barcodes;
        }

        /// <summary>
        /// Gets the barcode by paramcode.
        /// </summary>
        /// <param name="p_sBarcodeParamcode">The Barcode Paramcode.</param>
        /// <returns></returns>
        public static long GetBarCodeId(string p_sBarcodeParamcode)
        {
            return CABTranslator.GetBarCodeId(DA_CAB.GetBarCodeId(p_sBarcodeParamcode));
        }

        /// <summary>
        /// Gets the barcode document separtors by id.
        /// </summary>
        /// <param name="barcodeId">The barcode id.</param>
        /// <returns></returns>
        public static List<BarcodeDocumentSeparator> GetBarcodeDocumentSepartorsById(long barcodeId)
        {
            return CABTranslator.TranslateBarcodeDocumentSeparator(DA_CAB.GetBarcodeDocumentSepartorsById(barcodeId));
        }

        /// <summary>
        /// Gets the barcode folder separtors by id.
        /// </summary>
        /// <param name="barcodeId">The barcode id.</param>
        /// <returns></returns>
        public static List<BarcodeFolderSeparator> GetBarcodeFolderSepartorsById(long barcodeId)
        {
            return CABTranslator.TranslateBarcodeFolderSeparator(DA_CAB.GetBarcodeFolderSepartorsById(barcodeId));
        }

        #endregion Get

        #region Update

        /// <summary>
        /// Update the barcode.
        /// </summary>
        /// <param name="barcode">The barcode.</param>
        /// <returns></returns>
        public static long UpdateBarcode(Barcode barcode)
        {
            return DA_CAB.UpdateBarcode(barcode);
        }

        /// <summary>
        /// Update the barcode status List.
        /// </summary>
        /// <param name="p_sBarcodeIdList">The barcode id List.</param>
        /// <param name="p_bBarCodeStatus">The Barcode status to update</param>
        /// <returns></returns>
        public static bool UpdateStatusBarcodeList(string p_sBarcodeIdList, Boolean p_bBarCodeStatus)
        {
            return DA_CAB.UpdateStatusBarcodeList(p_sBarcodeIdList, p_bBarCodeStatus);
        }

        #endregion Update

        #endregion Interface Setting

        #region CheckUnicityIndex

        /// <summary>Check if the index value existe</summary>
        /// <returns>An boolean with the index is unique or not</returns>
        public static bool CheckUnicityIndexvalue(long p_lFolderTypeId, long p_lindexId, string p_sIndexValue, long p_lFolderId)
        {
            return CABTranslator.CheckUnicityIndexvalue(DA_CAB.CheckUnicityIndexvalue(p_lFolderTypeId, p_lindexId, p_sIndexValue, p_lFolderId));
        }

        #endregion CheckUnicityIndex
    }
}
